//
//  DanceMusic.m
//  gong
//
//  Created by echo on 14-3-4.
//  Copyright (c) 2014年 echo. All rights reserved.
//

#import "DanceMusic.h"

@implementation DanceMusic
{
    NSMutableArray *_strongBeatPositionArray;
    NSMutableArray *_weakBeatPositonArray;
    NSMutableArray *_secondStrongBeatPositionArray;
    NSMutableArray *_secondWeakBeatPositonArray;
    NSTimeInterval _checkTimeSpan;
    NSTimeInterval _checkTimeSpan1;
    NSTimeInterval _checkTimeSpan2;
    NSInteger _totalTouchCount;
    NSInteger _totalScore;
    
    NSInteger _normalBeatCount;
}

- (id)init
{
    self = [super init];
    if (self) {
        _strongBeatPositionArray = [NSMutableArray array];
        _weakBeatPositonArray = [NSMutableArray array];
        _secondStrongBeatPositionArray = [NSMutableArray array];
        _secondWeakBeatPositonArray = [NSMutableArray array];
    }
    
    return self;
}

- (void)calculateBeatPostions
{
    NSTimeInterval beatSpan = 60.0 / self.bpm;
    _normalBeatCount = self.duration / beatSpan;
    _checkTimeSpan = beatSpan / 6;
    _checkTimeSpan1 = beatSpan / 5;
    _checkTimeSpan2 = beatSpan / 4;

    NSInteger beatFlag = 0;
    
    for (NSTimeInterval beatPosition = beatSpan/2; beatPosition < self.duration; beatPosition += beatSpan) {
        if (TwoQuartersShot == self.beatType) {
            if (0 == beatFlag % self.beatType) {
                [_strongBeatPositionArray addObject:@(beatPosition)];
            } else {
                [_weakBeatPositonArray addObject:@(beatPosition)];
            }
        } else if (ThreeQuarterShot == self.beatType) {
            if (0 == beatFlag % self.beatType) {
                [_strongBeatPositionArray addObject:@(beatPosition)];
            } else if (1 == beatFlag % self.beatType) {
                [_weakBeatPositonArray addObject:@(beatPosition)];
            } else {
                [_secondWeakBeatPositonArray addObject:@(beatPosition)];
            }
        } else {
            if (0 == beatFlag % self.beatType) {
                [_strongBeatPositionArray addObject:@(beatPosition)];
            } else if (1 == beatFlag % self.beatType) {
                [_weakBeatPositonArray addObject:@(beatPosition)];
            } else if (2 == beatFlag % self.beatType) {
                [_secondStrongBeatPositionArray addObject:@(beatPosition)];
            } else {
                [_secondWeakBeatPositonArray addObject:@(beatPosition)];
            }
        }
        
        beatFlag++;
    }
    
//    NSLog(@"strong pos count is %@", _strongBeatPositionArray);
    _totalTouchCount = 0;
    _totalScore = 0;
}

- (NSInteger)evalue:(NSTimeInterval)touchPosition
{
    _totalTouchCount ++;
    if (TwoQuartersShot == self.beatType) {
        for (NSNumber *timePosition in _strongBeatPositionArray) {
            NSTimeInterval timeValue = [timePosition doubleValue];
            if (touchPosition > (timeValue - _checkTimeSpan) && (touchPosition < timeValue + _checkTimeSpan)) {
                NSLog(@"===Good== timeValue is %f, touch is %f", timeValue, touchPosition);
                _totalScore += 5;
                return 1;
            }
            
            if (touchPosition > (timeValue - _checkTimeSpan1) && (touchPosition < timeValue + _checkTimeSpan1)) {
                NSLog(@"\\===Good== timeValue is %f, touch is %f", timeValue, touchPosition);
                _totalScore += 3;
                return 2;
            }
            
            if (touchPosition > (timeValue - _checkTimeSpan2) && (touchPosition < timeValue + _checkTimeSpan2)) {
                NSLog(@"\\\\===Good== timeValue is %f, touch is %f", timeValue, touchPosition);
                _totalScore += 1;
                return 3;
            }
        }
        
//        for (NSNumber *timePosition in _weakBeatPositonArray) {
//            NSTimeInterval timeValue = [timePosition doubleValue];
//            if (touchPosition > (timeValue - _checkTimeSpan) && (touchPosition < timeValue + _checkTimeSpan)) {
//                NSLog(@"!!===Good== timeValue is %f, touch is %f", timeValue, touchPosition);
//                _totalScore += 10;
//                return 1;
//            }
//            
//            if (touchPosition > (timeValue - _checkTimeSpan1) && (touchPosition < timeValue + _checkTimeSpan1)) {
//                NSLog(@"\\!!===Good== timeValue is %f, touch is %f", timeValue, touchPosition);
//                _totalScore += 5;
//                return 2;
//            }
//
//            if (touchPosition > (timeValue - _checkTimeSpan2) && (touchPosition < timeValue + _checkTimeSpan2)) {
//                NSLog(@"\\\\===Good== timeValue is %f, touch is %f", timeValue, touchPosition);
//                _totalScore += 2;
//                return 3;
//            }
//            
//        }
    } else if (ThreeQuarterShot == self.beatType) {
        for (NSNumber *timePosition in _strongBeatPositionArray) {
            NSTimeInterval timeValue = [timePosition doubleValue];
            if (touchPosition > (timeValue - _checkTimeSpan) && (touchPosition < timeValue + _checkTimeSpan)) {
                NSLog(@"===============Good");
                _totalScore += 5;
                return 1;
            }
        }
        
        for (NSNumber *timePosition in _weakBeatPositonArray) {
            NSTimeInterval timeValue = [timePosition doubleValue];
            if (touchPosition > (timeValue - _checkTimeSpan) && (touchPosition < timeValue + _checkTimeSpan)) {
                NSLog(@"!=======So=======Good");
                _totalScore += 10;
                return 1;
            }
        }

        for (NSNumber *timePosition in _secondWeakBeatPositonArray) {
            NSTimeInterval timeValue = [timePosition doubleValue];
            if (touchPosition > (timeValue - _checkTimeSpan) && (touchPosition < timeValue + _checkTimeSpan)) {
                NSLog(@"!!!!!=======So=======Good");
                _totalScore += 15;
                return 1;
            }
        }

    }
    
    return 0;
}

- (NSInteger)getScore
{
    return _totalScore * _normalBeatCount / _totalTouchCount;
}

@end
